﻿// --------------------------------
// <copyright file="Button.cs" company="Faculty of Information Technology, Czech Technical University in Prague">
//     This document is shared by Microsoft Public License (MS-PL)
//     For further details please see http://msdn.microsoft.com/en-us/library/ff648068.aspx or license.txt.
// </copyright>
// <author>Lukáš Molnár</author>

// <email>molnaluk@fit.cvut.cz</email>

// <date>14. 2. 2012 0:13:46</date>

// <summary>
// 
// </summary>
// --------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Serialization;

#if WINDOWS

using System.ComponentModel;
using FITClub.TextureMenu.Configuration;
using System.Drawing.Design;
using FITClub.Episode1.Levels;

#endif

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace FITClub.TextureMenu
{
#if WINDOWS

    [System.Serializable]
#endif
    public class Button : AMenuItem, IUpdateableItem
    {
        [XmlElement]
#if WINDOWS
        [Category("Path")]
        [Editor(typeof(ContentPathEditor), typeof(UITypeEditor))]
        [EditorBrowsable(EditorBrowsableState.Always)]
#endif
        public string FontPath { get; set; }

        public string Text { get; set; }

        [XmlAttribute("Alignment")]
#if WINDOWS
        [Category("Design")]
#endif
        public TextAlignment Alignment { get; set; }

        [XmlElement]
#if WINDOWS
        [Category("Colors")]
        [Editor(typeof(ColorEditor), typeof(UITypeEditor))]
        [EditorBrowsable(EditorBrowsableState.Always)]
#endif
        public Color TextColor { get; set; }

        [XmlElement]
#if WINDOWS
        [Category("Colors")]
        [Editor(typeof(ColorEditor), typeof(UITypeEditor))]
        [EditorBrowsable(EditorBrowsableState.Always)]
#endif
        public Color BorderColor { get; set; }

        public int BorderWidth { get; set; }

        [XmlElement]
#if WINDOWS
        [Category("Colors")]
        [Editor(typeof(ColorEditor), typeof(UITypeEditor))]
        [EditorBrowsable(EditorBrowsableState.Always)]
#endif
        public Color BackgroundColor { get; set; }

        [XmlElement]
#if WINDOWS
        [Category("Colors")]
        [Editor(typeof(ColorEditor), typeof(UITypeEditor))]
        [EditorBrowsable(EditorBrowsableState.Always)]
#endif
        public Color Highlight { get; set; }

#if WINDOWS
        [System.NonSerialized]
#endif
        private SpriteFont font;
#if WINDOWS
        [System.NonSerialized]
#endif
        private Texture2D fill;

        protected bool drawhighlight;

        public override void LoadContent(Microsoft.Xna.Framework.Content.ContentManager loader)
        {
            if (this.ContentLoaded == false)
            {
                fill = MenuHelper.CreateFillTexture(this.BackgroundColor);
                this.font = MenuHelper.LoadFontOrDefault(loader, this.FontPath);
                if (Highlight == Color.Transparent)
                {
                    Highlight = Color.WhiteSmoke;
                }
                this.ContentLoaded = true;
            }
        }

        public override void Draw(Microsoft.Xna.Framework.Graphics.SpriteBatch spriteBatch, Color tint)
        {
            spriteBatch.Draw(fill, this.Bounds, tint);
            if (this.IsSelected)
            {
                MenuHelper.DrawBorder(spriteBatch, this.Bounds, Owner.SelectedItemBackground, 5);
            }
            if (drawhighlight)
            {
                MenuHelper.FillRectangle(spriteBatch, this.Bounds, this.Highlight);
            }
            MenuHelper.DrawText(spriteBatch, this.font, this.Text, this.TextColor, this.Bounds, this.Alignment, this.Resizable);
            if (this.BorderWidth > 0)
            {
                MenuHelper.DrawBorder(spriteBatch, new Rectangle(this.Bounds.X + this.BorderWidth, this.Bounds.Y + this.BorderWidth, this.Bounds.Width - 2 * this.BorderWidth, this.Bounds.Height - 2 * this.BorderWidth), this.BorderColor, this.BorderWidth);
            }
        }

        public override void Dispose()
        {
            if (this.ContentLoaded)
            {
                fill.Dispose();
                this.ContentLoaded = false;
            }
        }

        public override void ItemLeft()
        {
            drawhighlight = false;
        }

        public void Update(GameTime gt)
        {
            drawhighlight = MenuManager.Current.TapTest(this.Bounds) || MenuManager.Current.MouseDownTest(this.Bounds);
        }
    }
}